Skip to content

Completed Design-2 - #2493

Open
Keerthi0910 wants to merge 3 commits into
super30admin:masterfrom
Keerthi0910:master
Open

Completed Design-2#2493
Keerthi0910 wants to merge 3 commits into
super30admin:masterfrom
Keerthi0910:master

Conversation

@Keerthi0910

Copy link
Copy Markdown

@Keerthi0910 Keerthi0910 changed the title queue changes added Completed Design-2 Aug 2, 2026
@super30admin

Copy link
Copy Markdown
Owner

Implement Queue using Stacks (Queue.java)

Strengths:

  1. Your solution correctly implements the queue using two stacks with optimal amortized O(1) time complexity
  2. Good use of comments to document your approach and time/space complexity
  3. Defensive programming with the empty check in pop and peek is a nice touch
  4. Clear variable naming makes the code easy to understand

Areas for Improvement:

  1. Code Duplication: The transfer logic from inStack to outStack is duplicated in both pop and peek. Consider extracting this into a private helper method like private void transfer() to reduce duplication and improve maintainability.
  2. Java Best Practice: In Java, Stack is a legacy class. Consider using Deque<Integer> with ArrayDeque implementation instead, which is more efficient and recommended for stack-like behavior. For example: Deque<Integer> inStack = new ArrayDeque<>();
  3. Method Reuse: You could call peek() from pop() to avoid duplicating the transfer logic, similar to the reference solution.

Example Refactoring:

private void transfer() {
    while (!inStack.isEmpty()) {
        outStack.push(inStack.pop());
    }
}

public int pop() {
    if (empty()) return -1;
    if (outStack.isEmpty()) transfer();
    return outStack.pop();
}

public int peek() {
    if (empty()) return -1;
    if (outStack.isEmpty()) transfer();
    return outStack.peek();
}

VERDICT: PASS


Design HashMap (HashMap.java)

EESSENTIAL DETAILS: The student used 10000 buckets instead of 1000, but this is a minor difference and doesn't affect correctness. The logic is essentially identical to the reference solution. The student correctly implemented all four operations (put, get, remove) using a hash table with separate chaining (linked lists). The code is well-structured and readable. The student's solution is correct and efficient.

ESSENTIAL DETAILS: The student used 10000 buckets instead of 1000, but this is a minor difference and doesn't affect correctness. The logic is essentially identical to the reference solution. The student correctly implemented all all four operations (put, get, remove) using a hash table with separate chaining (linked lists). The code is well-structured and readable. The student's solution is correct and efficient.

VERDICT: NEEDS_IMPROVEMENT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants